using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;


namespace ForumExplorer.Core.ObjectModel
{
    public enum ForumThreadType
    {
        Question,
        Comment
    }

    public class DocumentMetaData
    {
        public void Parse(HtmlDocument doc)
        {
            HtmlElementCollection tags = doc.GetElementsByTagName(@"meta");
            string name;
            foreach (HtmlElement element in tags)
            {
                name = element.GetAttribute("name");
                switch (name)
                {
                    case "Search.MSForums.SiteID":
                        {
                            _SiteID = Convert.ToInt32(element.GetAttribute("content"));
                            break;
                        }
                    case "Search.MSForums.GroupID":
                        {
                            _GroupID = Convert.ToInt32(element.GetAttribute("content"));
                            break;
                        }
                    case "Search.MSForums.GroupName":
                        {
                            _GroupName = element.GetAttribute("content");
                            break;
                        }
                    case "Search.MSForums.ForumID":
                        {
                            _ForumID = Convert.ToInt32(element.GetAttribute("content"));
                            break;
                        }
                    case "Search.MSForums.ForumName":
                        {
                            _ForumName = element.GetAttribute("content");
                            break;
                        }
                    case "Search.MSForums.ThreadID":
                        {
                            _ThreadID = Convert.ToInt32(element.GetAttribute("content"));
                            break;
                        }
                    case "Search.MSForums.ThreadName":
                        {
                            _ThreadName = element.GetAttribute("content");
                            break;
                        }
                    case "Search.MSForums.IsAnswered":
                        {
                            string strContent = element.GetAttribute("content");
                            _IsAnswered = (0 == String.Compare(strContent, "1", true));
                            break;
                        }
                    case "Search.MSForums.IsQuestion":
                        {
                            string strContent = element.GetAttribute("content");
                            switch (0 == String.Compare(strContent, "1", true))
                            {
                                case true:
                                    {
                                        _Type = ForumThreadType.Question;
                                        break;
                                    }
                                case false:
                                    {
                                        _Type = ForumThreadType.Comment;
                                        break;
                                    }
                            }
                            break;
                        }
                    case "Search.MSForums.HelpfulThread":
                        {
                            string strContent = element.GetAttribute("content");
                            _HelpfulThread = (0 == String.Compare(strContent, "1", true));
                            break;
                        }
                    case "Search.MSForums.ThreadStarterUserID":
                        {
                            if (null == _ThreadStarterUser)
                            {
                                _ThreadStarterUser = new ForumUser();
                            }
                            _ThreadStarterUser.ID = Convert.ToInt32(element.GetAttribute("content"));
                            break;
                        }
                    case "Search.MSForums.ThreadStarterUserName":
                        {
                            if (null == _ThreadStarterUser)
                            {
                                _ThreadStarterUser = new ForumUser();
                            }
                            _ThreadStarterUser.Name = element.GetAttribute("content");
                            break;
                        }
                    case "Search.MSForums.ThreadContributorUserID":
                        {
                            _nContributorUserIDLast = Convert.ToInt32(element.GetAttribute("content"));
                            if (false == _ContributorUsers.ContainsKey(_nContributorUserIDLast))
                            {
                                _ContributorUsers.Add(_nContributorUserIDLast, String.Empty);
                            }
                            break;
                        }
                    case "Search.MSForums.ThreadContributorUserName":
                        {
                            _ContributorUsers[_nContributorUserIDLast] = element.GetAttribute("content");
                            break;
                        }
                    case "Search.MSForums.ThreadAnswererUserID":
                        {
                            _nAnswererIDLast = Convert.ToInt32(element.GetAttribute("content"));
                            if (false == _AnswererUsers.ContainsKey(_nAnswererIDLast))
                            {
                                _AnswererUsers.Add(_nAnswererIDLast, String.Empty);
                            }
                            break;
                        }
                    case "Search.MSForums.ThreadAnswererUserName":
                        {
                            _AnswererUsers[_nAnswererIDLast] = element.GetAttribute("content");
                            break;
                        }
                    default:
                        {
                            // Unknown meta tag!
                            break;
                        }
                }
            }
        }

        public int SiteID
        {
            get
            {
                return _SiteID;
            }
        }

        public int ForumID
        {
            get
            {
                return _ForumID;
            }
        }

        public int ThreadID
        {
            get
            {
                return _ThreadID;
            }
        }

        private int _SiteID;
        private int _GroupID;
        private string _GroupName;
        private int _ForumID;
        private string _ForumName;
        private int _ThreadID;
        private string _ThreadName;
        private bool _IsAnswered;
        private ForumThreadType _Type;
        private bool _HelpfulThread;
        private ForumUser _ThreadStarterUser;
        private Dictionary<int, string> _ContributorUsers = new Dictionary<int, string>();
        private int _nContributorUserIDLast;
        private Dictionary<int, string> _AnswererUsers = new Dictionary<int, string>();
        private int _nAnswererIDLast;
    }
}
